﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using DevExpress.Web.ASPxPivotGrid;

public partial class WebUserControl_CtrBaoCaoCapQuanLyMoi : System.Web.UI.UserControl
{
    static string  error = "";
   
    static DataTable dataCap;
    static string usertype = "";
    ClassExecute.ClassExecute dt1;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
            dt1 = new ClassExecute.ClassExecute();
            usertype = dt1.GetDataDepend(str, "GET_USERTYPE", CommandType.StoredProcedure, ref error, new SqlParameter("@UserName", Session["username"].ToString()));

            try
            {
                BindMonthList();
                BindTTPP();
                if (usertype == "ad")
                {
                    DropDownList2.SelectedValue = Session["sitecode"].ToString();
                    DropDownList2.Enabled = true;
                }
                else
                {
                    DropDownList2.SelectedValue = Session["sitecode"].ToString();
                    DropDownList2.Enabled = false;

                }
            }
            catch (Exception ex)
            {
                uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
            }
        }
        else
        {
          
            try
            {
                Title();
                this.ASPxPivotGrid1.DataSource = dataCap;
                this.ASPxPivotGrid1.DataBind();
            }
            catch
            {
                return;
            }
        
        }
    }
    void BindMonthList()
    {
        dt1 = new ClassExecute.ClassExecute();
        DataTable db = dt1.Get_Data_DataTable(str, ref error, "select Names,[Values] from MonthListReport order by  right(Names,4)+left(Names,3) desc", CommandType.Text);
        DropDownList1.DataSource = db;
        DropDownList1.DataTextField = "Names";
        DropDownList1.DataValueField = "Values";
        DropDownList1.DataBind();

    }
    void BindTTPP()
    { 
        dt1 = new ClassExecute.ClassExecute();
        DataTable db = dt1.Get_Data_DataTable(str, ref error, "GET_SITENAME_ALL", CommandType.StoredProcedure);
        DropDownList2.DataSource = db;
        DropDownList2.DataTextField = "sitename";
        DropDownList2.DataValueField = "Sitecode";
        DropDownList2.DataBind();

    }
    public void Title()
    {
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Baocao"]))
        {
            ASPxPivotGrid1.Fields.Add("Baocao", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Baocao"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Baocao"].Caption = "Báo Cáo Cấp Quản Lý Mới";
        }
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["FromDate"]))
        {
            ASPxPivotGrid1.Fields.Add("FromDate", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["FromDate"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["FromDate"].Caption = "TTPP : " + DropDownList1.SelectedItem.Text;
        }
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Time"]))
        {
            ASPxPivotGrid1.Fields.Add("Time", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Time"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Time"].Caption = "Ngày giờ lấy báo cáo : " + DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString();
        }
    }
    protected void bt_baocao_Click(object sender, EventArgs e)
    {
        ASPxPivotGrid1.Fields.Clear();
        Title();
        dt1 = new ClassExecute.ClassExecute();
        dataCap = dt1.Get_Data_DataTable(str, ref error, "GetBUMforReport", CommandType.StoredProcedure, new SqlParameter("@username", Session["username"].ToString()), new SqlParameter("@sitecode", DropDownList2.SelectedValue.ToString()), new SqlParameter("@YyyyMm", DropDownList1.SelectedValue.ToString()));

        foreach (DataColumn col in dataCap.Columns)
        {
            if ((col.ColumnName.ToString() == "DS cá nhân") || (col.ColumnName.ToString() == "DS nhóm cá nhân") || (col.ColumnName.ToString() == "DS toàn nhóm"))
            {
                ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.DataArea);
                ASPxPivotGrid1.Fields[col.ColumnName.ToString()].CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                ASPxPivotGrid1.Fields[col.ColumnName.ToString()].CellFormat.FormatString = "{0:#,###}";

            }
            else
            {
                ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.RowArea);

            }

        }

        this.ASPxPivotGrid1.DataSource = dataCap;
        this.ASPxPivotGrid1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (MemoryStream filestream = new MemoryStream())
        {
            string contentType = "application/ms-excel";
            string fileName = "Baocaocapquanlymoi.xls";
            ASPxPivotGridExporter1.ExportToXls(filestream);
            byte[] buffer = filestream.GetBuffer();
            Response.Clear();
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", contentType);
            Response.AppendHeader("Content-Transfer-Encoding", "binary");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.BinaryWrite(buffer);
            Response.End();
        }
    }
}
